﻿using System;
using System.Collections.Generic;
using ExtendedMvc.Html;

namespace ExtendedMvc.Elements {
	/// <summary>
	/// Column for the grid
	/// </summary>
	public abstract class GridCommandBase<TCommand, TModel> : IGridCommandBase<TModel>
		where TCommand : GridCommandBase<TCommand, TModel>
		where TModel : class {
		private IModelViewHelper<IEnumerable<TModel>> _helper;
		private GridCommandPosition _position = GridCommandPosition.DataRow;
		private string _text;
		private Func<TModel, bool> _showIf = x => true;
		private Func<TModel, bool> _enabledIf = x => true;

		protected IModelViewHelper<IEnumerable<TModel>> helper { get { return _helper; } }
		protected Func<TModel, bool> showIf { get { return _showIf; } }
		protected Func<TModel, bool> enabledIf { get { return _enabledIf; } }

		public GridCommandBase(IModelViewHelper<IEnumerable<TModel>> helper, string text) {
			_helper = helper;
			_text = text;
		}

		public virtual string Text { get { return _text; } }

		public virtual GridCommandPosition Position { get { return _position; } }

		public TCommand ShowIf(Func<TModel, bool> func) {
			_showIf = func;
			return (TCommand)this;
		}

		public TCommand EnabledIf(Func<TModel, bool> func) {
			_enabledIf = func;
			return (TCommand)this;
		}

		public TCommand DisplayIn(GridCommandPosition value) {
			_position = value;
			return (TCommand)this;
		}

		/// <summary>
		/// Gets the value for a particular cell in this column
		/// </summary>
		/// <param name="instance">Instance from which the value should be obtained</param>
		/// <returns>Item to be rendered</returns>
		public abstract string GetLink();

		IGridCommandBase<TModel> IGridCommandBase<TModel>.EnabledIf(Func<TModel, bool> func) {
			return this.EnabledIf(func);
		}

		IGridCommandBase<TModel> IGridCommandBase<TModel>.ShowIf(Func<TModel, bool> func) {
			return this.ShowIf(func);
		}
	}
}
